'use strict';

exports.__esModule = true;

var _dom = require('../utils/dom');

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Transition = function () {
  function Transition() {
    _classCallCheck(this, Transition);
  }

  Transition.prototype.beforeEnter = function beforeEnter(at) {
    (0, _dom.addClass)(at, 'collapse-transition');
    if (!at.dataset) at.dataset = {};

    at.dataset.oldPaddingTop = at.style.paddingTop;
    at.dataset.oldPaddingBottom = at.style.paddingBottom;

    at.style.height = '0';
    at.style.paddingTop = 0;
    at.style.paddingBottom = 0;
  };

  Transition.prototype.enter = function enter(at) {
    at.dataset.oldOverflow = at.style.overflow;
    if (at.scrollHeight !== 0) {
      at.style.height = at.scrollHeight + 'px';
      at.style.paddingTop = at.dataset.oldPaddingTop;
      at.style.paddingBottom = at.dataset.oldPaddingBottom;
    } else {
      at.style.height = '';
      at.style.paddingTop = at.dataset.oldPaddingTop;
      at.style.paddingBottom = at.dataset.oldPaddingBottom;
    }

    at.style.overflow = 'hidden';
  };

  Transition.prototype.afterEnter = function afterEnter(at) {
    // for safari: remove class then reset height is necessary
    (0, _dom.removeClass)(at, 'collapse-transition');
    at.style.height = '';
    at.style.overflow = at.dataset.oldOverflow;
  };

  Transition.prototype.beforeLeave = function beforeLeave(at) {
    if (!at.dataset) at.dataset = {};
    at.dataset.oldPaddingTop = at.style.paddingTop;
    at.dataset.oldPaddingBottom = at.style.paddingBottom;
    at.dataset.oldOverflow = at.style.overflow;

    at.style.height = at.scrollHeight + 'px';
    at.style.overflow = 'hidden';
  };

  Transition.prototype.leave = function leave(at) {
    if (at.scrollHeight !== 0) {
      // for safari: add class after set height, or it will jump to zero height suddenly, weired
      (0, _dom.addClass)(at, 'collapse-transition');
      at.style.height = 0;
      at.style.paddingTop = 0;
      at.style.paddingBottom = 0;
    }
  };

  Transition.prototype.afterLeave = function afterLeave(at) {
    (0, _dom.removeClass)(at, 'collapse-transition');
    at.style.height = '';
    at.style.overflow = at.dataset.oldOverflow;
    at.style.paddingTop = at.dataset.oldPaddingTop;
    at.style.paddingBottom = at.dataset.oldPaddingBottom;
  };

  return Transition;
}();

exports.default = {
  name: 'AtCollapseTransition',
  functional: true,
  render: function render(h, _ref) {
    var children = _ref.children;

    var data = {
      on: new Transition()
    };

    return h('transition', data, children);
  }
};